﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository.Sys;
using Model.BusinessModel;
using Model;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Infrastructure.Repository.Sys
{
    public class SysDataRuleRepository : Repository<SysDataRule>, ISysDataRuleRepository
    {

        /// <summary>
        /// 获取菜单数据规则数据
        /// </summary>
        /// <param name="menuId">菜单ID</param>
        /// <param name="ruleType">规则类型</param>
        /// <returns>返回规则数据</returns>
        public List<SysDataRuleOutPut> GetMenuDataRuleList(string menuId, int ruleType)
        {
            StringBuilder sql = new(@" SELECT sd.RuleId,sd.RuleName,sd.RuleType,sd.RuleJson,sd.AnalysisRule,sd.AnalysisRule_CN,sd.IsOpen,sd.Remark,sd.Sort,sd.CreateTime,sd.CreateUser,su.UserName from Sys_DataRule as sd 
                inner join  Sys_User as su on sd.CreateUser = su.UserId
               ");
            var qp = new QueryParameter();

            if (!string.IsNullOrEmpty(menuId))
            {
                qp.listWhere.Append(" where sd.MenuId = @MenuId ");
                qp.dynamicParameter.Add("@MenuId", menuId);
            }

            if (ruleType != 0)
            {
                qp.listWhere.Append(" and sd.RuleType = @RuleType ");
                qp.dynamicParameter.Add("@RuleType", ruleType);
            }

            if (!string.IsNullOrEmpty(qp.listWhere.ToString()))
            {
                sql.Append(qp.listWhere.ToString());
            }
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysDataRuleOutPut>(sql.ToString(), qp.dynamicParameter).ToList();
        }

        /// <summary>
        /// 根据规则id获取数据
        /// </summary>
        /// <param name="ruleId">规则id</param>
        /// <param name="menuId">菜单ID</param>
        /// <returns></returns>
        public SysDataRule? GetDataRuleByRuleIdOrMenuId(string ruleId, string menuId)
        {
            string sql = @" select *  from  Sys_DataRule  where RuleId = @RuleId and MenuId=@MenuId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.QueryFirstOrDefault<SysDataRule>(sql, new { RuleId = ruleId, MenuId = menuId });
        }

        /// <summary>
        /// 获取当前规则使用按钮
        /// </summary>
        /// <param name="ruleId"></param>
        /// <returns></returns>
        public List<string> GetUseDataRuleButton(string ruleId)
        {
            string sql = @" select sb.ButtonId from Sys_DataRule as sd
                            inner join Sys_Button as sb on sd.RuleId = sb.RuleId and sd.MenuId = sb.MenuId
                            where sd.RuleId =@RuleId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<string>(sql, new { RuleId = ruleId }).ToList();

        }

        /// <summary>
        /// 批量更新IsOpen字段
        /// </summary>
        /// <param name="menuId">菜单id</param>
        /// <param name="isOpen">是否开启</param>
        /// <param name="ruleType">规则类型</param>
        public void UpdateIsOpenByMenuId(string menuId, bool isOpen, int ruleType)
        {
            string sql = " update Sys_DataRule set IsOpen=@IsOpen  where MenuId =@MenuId  and RuleType =@RuleType  ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, new { MenuId = menuId, IsOpen = isOpen, RuleType = ruleType });
        }

        /// <summary>
        /// 批量更新IsOpen字段
        /// </summary>
        /// <param name="ruleId">规则id</param>
        /// <param name="isOpen">是否开启</param>
        /// <param name="ruleType">规则类型</param>
        public void UpdateIsOpenById(string ruleId, bool isOpen, int ruleType)
        {
            string sql = " update  Sys_DataRule set IsOpen=@IsOpen  where RuleId =@RuleId  and RuleType =@RuleType ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, new { RuleId = ruleId, IsOpen = isOpen, RuleType = ruleType });
        }
    }
}
